package cn.airfei.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * @description:
 * @author: air
 * @create: 2021-01-19 10:04
 */
public class NettyByteBuf {
    public static void main(String[] args) {

        // 创建一个对象,该对象包含一个数组arr,
        ByteBuf byteBuf = Unpooled.buffer(10);

        for (int i = 0; i < 10; i++) {
            byteBuf.writeByte(i);
        }

        for (int i = 0; i < byteBuf.capacity(); i++) {
            System.out.println(byteBuf.getByte(i));
            byteBuf.readByte();
        }


        ByteBuf copiedBuffer = Unpooled.copiedBuffer("hello air", CharsetUtil.UTF_8);

        if (copiedBuffer.hasArray()){
            System.out.println(copiedBuffer.array());
            System.out.println(new String(copiedBuffer.array(),CharsetUtil.UTF_8));

            System.out.println("copiedBuffer:"+copiedBuffer);
            System.out.println(copiedBuffer.readerIndex());
            System.out.println(copiedBuffer.writerIndex());
            System.out.println(copiedBuffer.arrayOffset());
            System.out.println(copiedBuffer.capacity());
            System.out.println(copiedBuffer.readableBytes());
            System.out.println(copiedBuffer.readByte());
            System.out.println(copiedBuffer.readableBytes());

        }
    }

}
